import Vue from "vue"
import Cookies from "js-cookie"
import Router from "vue-router"
// import type from "@/router/deployCode"

import statisticsQueryRouter from "./statisticsQuery/index.js"
import comparisonLibraryRouter from "./comparisonManage/index.js"
import deviceManageRouter from "./deviceMange/index.js"
import residentManageRouter from "./residentManage/index.js"
import investigateBusinessRouter from "./investigateBusiness/index.js"

// import purchaseRouter from "./purchase/index.js"
// import productRouter from "./product/index.js"

Vue.use(Router)
const login = (resolve) => require(["@/views/other/login/index"], resolve)
const panoramicData = (resolve) => require(["@/views/panoramicData/index"], resolve)
const panoramicDataDark = (resolve) => require(["@/views/panoramicDataDark/index"], resolve)
const statisticsQueryManage = (resolve) => require(["@/views/statisticsQuery/index"], resolve)
const comparisonLibrary = (resolve) => require(["@/views/comparisonManage/index"], resolve)
const deviceManage = (resolve) => require(["@/views/deviceManage/index"], resolve)
const residentManage = (resolve) => require(["@/views/residentManage/index"], resolve)
const investigateBusiness = (resolve) => require(["@/views/investigateBusiness/index"], resolve)

// const productManage = (resolve) => require(["@/views/productManage/index"], resolve)
// const PreviewFile = (resolve) => require(["@/views/supplierManage/supplier/PreviewFile"], resolve)

export const routes = [
  {
    path: "statisticsQuery", // 统计查询
    name: "statisticsQueryManage",
    component: statisticsQueryManage,
    children: statisticsQueryRouter,
    meta: {
      title: "统计查询",
      isAllways: true
    }
  },
  {
    path: "comparisonLibrarys", // 比对库管理
    name: "comparisonLibrarys",
    component: comparisonLibrary,
    children: comparisonLibraryRouter,
    meta: {
      title: "比对库管理",
      isAllways: true
    }
  },
  {
    path: "deviceManages", // 设备管理
    name: "deviceManages",
    component: deviceManage,
    children: deviceManageRouter,
    meta: {
      title: "设备管理",
      isAllways: true
    }
  },
  {
    path: "residentManage", // 设备管理
    name: "residentManage",
    component: residentManage,
    children: residentManageRouter,
    meta: {
      title: "住户分析",
      isAllways: true
    }
  },
  {
    path: "investigateBusiness", // 侦查业务
    name: "investigateBusiness",
    component: investigateBusiness,
    children: investigateBusinessRouter,
    meta: {
      title: "侦查业务",
      isAllways: true
    }
  }
]
export const constantRoutes = [
  {
    path: "",
    component: () => import("@/components/layout/index"),
    name: "Index",
    meta: {
      title: "首页",
      isAllways: true,
      insIndex: true
    },
    redirect: {
      name: "panoramicData"
    },
    children: routes
  },
  {
    path: "/panoramicData",
    component: panoramicData,
    name: "panoramicData",
    meta: {
      isLogin: true,
      title: "全景数据"
    }
  },
  {
    path: "/panoramicDataDark",
    component: panoramicDataDark,
    name: "panoramicDataDark",
    meta: {
      isLogin: true,
      title: "全景数据"
    }
  },
  {
    path: "/login",
    component: login,
    meta: {
      isLogin: true,
      title: "登录"
    }
  },
  {
    path: "*",
    component: () => import("@/views/other/error/404"),
    meta: {
      title: "404"
    }
  }
]
// 导出路由 在 main.js 里使用
const createRouter = () =>
  new Router({
    mode: "history",
    // base: window.getServerJson.context,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

router.beforeEach((to, from, next) => {
  const userInfo = Cookies.get("token")

  if (userInfo) {
    // 如果存在user信息，进行跳转
    console.log("有user信息")
    next()
  } else if (to.path === "/login") {
    // 否则，跳转至登录页面
    next() // 假设跳转到login页面
  } else {
    next("/login")
  }
})

// router.beforeEach((to, from, next) => {
// must call `next`
// console.log(to, from)
// if (to.path === "/custom/salesLead") {
//   next()
// } else {
// if (to.meta.requireAuth) {
//   next({
//     path: "/login",
//     query: { redirect: to.fullPath }
//   })
// } else {
//   next()
// }
// }
// })

export default router
